package com.qfedu.repo.controller;

import com.alibaba.fastjson.JSON;
import com.qfedu.repo.bean.Manager;
import com.qfedu.repo.bean.RespResult;
import com.qfedu.repo.common.Const;
import com.qfedu.repo.dao.ManagerDao;
import com.qfedu.repo.dao.impl.ManagerDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * projectName:QfeduJava2401
 * author:jww
 * time:2024/5/8 21:46
 * description:
 */
@WebServlet(name = "ManagerServlet", urlPatterns = "/manager")
public class ManagerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-type", "text/html;charset=UTF-8");
        response.setContentType("application/json;charset=UTF-8");

        //1.获取到method
        String method = request.getParameter("method");
        //login、logout、modify_permission(修改权限)
        RespResult result = null;
        switch (method) {
            case "login":
                result = login(request, response);
                break;
            default:
                break;
        }
        PrintWriter writer = response.getWriter();

        String jsonStr = JSON.toJSONString(result);
        System.out.println(jsonStr);
        writer.write(jsonStr);
        writer.flush();
        writer.close();
    }

    /**
     * 后台管理员的登录方法
     *
     * @param request  请求
     * @param response 响应
     */
    private RespResult login(HttpServletRequest request, HttpServletResponse response) {
        RespResult result = new RespResult();

        //1、获取参数
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        //1.5、参数的合法性验证码
        PrintWriter writer = null;
        if (account.length() < 4 || account.length() > 12) {
            try {
                result.setCode(Const.LOGIN_INVALID);
                result.setMsg("参数不合法");
                writer = response.getWriter();
                writer.write("参数不合法");
                writer.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        //2、调用业务逻辑处理
        ManagerDao managerDao = new ManagerDaoImpl();
        Manager manager = managerDao.login(account, password);
        //3、判断结果
        if (manager == null) {
            //返回 登录失败的结果
            //"登录失败"
            result.setCode(Const.LOGIN_FAIL);
            result.setMsg("failed");
        } else {
            //返回 登录成功的结果
            result.setCode(Const.LOGIN_SUCCESS);
            result.setMsg("success");
            //manager对象返回：
            //fastjson、gson
            result.setData(JSON.toJSONString(manager));

        }
        //4、返回前端
//        writer.write(JSON.toJSONString(result));
//        writer.flush();
//        writer.close();
        return result;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}


